/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package listeners;

import diagram.connections.TriggerConnection;
import diagram.handles.ConnectHandle;
import diagram.locations.AbsoluteLocation;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import model.DiagramModel;

/**
 *
 * @author Jelle
 */
public class TimerListener implements ActionListener {

    private DiagramModel model;
    private TriggerConnection c;
    private ConnectHandle hc;
    private int b,  h;

    public TimerListener(TriggerConnection c, int b, int h, ConnectHandle hc, DiagramModel model) {
        this.c = c;
        this.b = b;
        this.h = h;
        this.hc = hc;
        this.model = model;
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("timer ation");
        int xx = 0, yy = 0;

        if(b==0 && h == 0){
            return;
        }

        if (b > 0) {
            b--;
            xx=b;
        }
        if (h > 0) {
            h--;
            yy=h;
        }
        if (b < 0) {
            b++;
            xx=b;
        }
        if (h < 0) {
            h++;
            yy=h;
        }
        if (xx < 0 ) {
            c.setTarget(new AbsoluteLocation(c.getTarget().getX() + 1, c.getTarget().getY()));
            System.out.println("perf XX<0");

        }
        if (yy < 0 ) {
            c.setTarget(new AbsoluteLocation(c.getTarget().getX(), c.getTarget().getY()+1));
            System.out.println("perf YY<0");

        }
        if (xx > 0 ) {
            c.setTarget(new AbsoluteLocation(c.getTarget().getX() -1, c.getTarget().getY()));
            System.out.println("perf XX>0");

        }
        if (yy > 0 ) {
            c.setTarget(new AbsoluteLocation(c.getTarget().getX(), c.getTarget().getY()-1));
            System.out.println("perf YY>0");

        }
        if(yy == 0 && xx == 0){
            model.removeObject(c);
            model.addObject(hc);
            Timer t = (Timer)e.getSource();
            t.stop();
                                          
        }
    }
}
